package com.example.blog.util;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Classname FastDfsFileUtil
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/4/6 14:42
 * @Created by wlh12
 */
@Component
public class FastDfsFileUtil {
    @Autowired
    private FastFileStorageClient storageClient;
    /**
     * 上传
     * @param file
     * @return
     */
    public String upload(MultipartFile file){
        String filename = file.getOriginalFilename();
        String extName = filename.substring(filename.lastIndexOf(".") + 1);
        StorePath storePath = null;
        try {
            storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            return storePath.getFullPath();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
